package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
   @Autowired
   private AgencyCertificationAuditMapper agencyCertificationAuditMapper;
   @Autowired
    private IAgencyCertificationService agencyCertificationService;
    /**
     * 核服务人员认证分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<AgencyCertificationAuditResDTO> pagedto(AgencyCertificationAuditPageQueryReqDTO dto) {
        Page<AgencyCertificationAudit> page = PageUtils.parsePageQuery(dto, AgencyCertificationAudit.class);
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = new LambdaQueryWrapper<AgencyCertificationAudit>()
                .eq(!ObjectUtil.isEmpty(dto.getAuditStatus()), AgencyCertificationAudit::getAuditStatus, dto.getAuditStatus())
                .eq(!ObjectUtil.isEmpty(dto.getCertificationStatus()), AgencyCertificationAudit::getCertificationStatus, dto.getCertificationStatus())
                .like(!ObjectUtil.isEmpty(dto.getName()), AgencyCertificationAudit::getName, dto.getName())
                .like(!ObjectUtil.isEmpty(dto.getLegalPersonName()), AgencyCertificationAudit::getLegalPersonName, dto.getLegalPersonName());
        Page<AgencyCertificationAudit> agencyCertificationAuditPage = agencyCertificationAuditMapper.selectPage(page, queryWrapper);
        return PageUtils.toPage(agencyCertificationAuditPage, AgencyCertificationAuditResDTO.class);
    }

    @Override
    public void applyCertification(AgencyCertificationAuditResDTO agencyCertificationAuditAddReqDTO) {
        AgencyCertificationAudit agencyCertificationAudit = BeanUtil.toBean(agencyCertificationAuditAddReqDTO,AgencyCertificationAudit.class);
        BeanUtil.toBean(agencyCertificationAuditAddReqDTO,AgencyCertificationAudit.class);
        baseMapper.insert(agencyCertificationAudit);
        Long serveProviderId =agencyCertificationAuditAddReqDTO.getServeProviderId();
        AgencyCertification agencyCertification =agencyCertificationService.getById(serveProviderId);
        if(ObjectUtil.isNotEmpty(agencyCertification)){
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());//认证中
            agencyCertificationService.updateById(agencyCertification);
        }else{
            agencyCertification=new AgencyCertification();
            agencyCertification.setId(serveProviderId);
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());//认证中
            agencyCertificationService.save(agencyCertification);

        }
    }

    @Override
    public RejectReasonResDTO queryCurrentUserLastRejectReason() {
        AgencyCertificationAudit agencyCertificationAudit =baseMapper.selectOne(new LambdaQueryWrapper<AgencyCertificationAudit>().eq(AgencyCertificationAudit::getServeProviderId, UserContext.currentUserId()).orderByDesc(AgencyCertificationAudit::getCreateTime).last("limit 1"));
        return new RejectReasonResDTO(agencyCertificationAudit.getRejectReason());


    }


}
